package com.tangtang.gulimall.product.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.tangtang.gulimall.product.Grops.AddGrops;
import com.tangtang.gulimall.product.Vo.AttrGroupRelationVo;
import com.tangtang.gulimall.product.Vo.AttrWithattrVo;
import com.tangtang.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.tangtang.gulimall.product.entity.AttrEntity;
import com.tangtang.gulimall.product.entity.CategoryEntity;
import com.tangtang.gulimall.product.service.AttrAttrgroupRelationService;
import com.tangtang.gulimall.product.service.AttrService;
import com.tangtang.gulimall.product.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.tangtang.gulimall.product.entity.AttrGroupEntity;
import com.tangtang.gulimall.product.service.AttrGroupService;
import com.tangtang.common.utils.PageUtils;
import com.tangtang.common.utils.R;



/**
 * 属性分组
 *
 * @author tangtang
 * @email a1104707271@vip.qq.com
 * @date 2020-09-11 15:22:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getwithattr(@PathVariable("catelogId") Long catelogId)
    {
        //根据传过来的分类id，获取所有分类的属性
        List<AttrWithattrVo> attrWithattrVoList = attrGroupService.getallattrWithattr(catelogId);
        return R.ok().put("data",attrWithattrVoList);
    }


    /**
     * 新增关联，是一个数组
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addAttrattrgrouprelation(@RequestBody List<AttrGroupRelationVo> vos)
    {
        for (AttrGroupRelationVo attrGroupRelationVo:vos){
            System.out.println(attrGroupRelationVo.getAttrId()+":"+attrGroupRelationVo.getAttrGroupId());
        }
        //保存
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 查询自身没有关联的属性
     * @param params
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R addrelation(@RequestParam Map<String,Object> params,@PathVariable("attrgroupId") Long attrgroupId)
    {
        PageUtils page = attrService.getnoatterelation(params,attrgroupId);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation/delete")
    public R attrgroupDelete(@RequestBody AttrGroupRelationVo[] Vos)
    {
        attrService.deleteattrgroup(Vos);
        return R .ok();
    }

    //根据分组id回显分组信息
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRrelation(@PathVariable("attrgroupId") Long attrgroupId){
        //System.out.println("有请求进入属性关联！"+attrgroupId);
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntities);
    }


    /**
     * 列表，因为这一块给的一个参数是一个路径的值{catagroyid}
     * 所以用注解：
     * @PathVariable("catagroyid")
     * 来接收这个参数
     */
    @RequestMapping("/list/{catagroyid}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catagroyid") Long catagroyid){
        //页面提交完成后刷新这个list
        //返回了所有分页的id
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catagroyid);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//根据获取的id进行精确查询
		Long attrgroupid = attrGroup.getCatelogId();
		System.out.println("获取精确的id为："+attrgroupid);
		Long[] path = categoryService.findCatelogpath(attrgroupid);
		//再携一个根据当前id查询的所有父级id的catelogpath完整路径，进行put发送。
        //可以根据当前传过来的id进行查询父类的id
        attrGroup.setCatelogPath(path);//将查询到的路径放入数组中put出去
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存,保存的时候进行数据的后端校验
     * 如果没有添加或者更新这些class，则可以直接使用
     * @Vaild
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGrops.class) @RequestBody AttrGroupEntity attrGroup){
        System.out.println("新增所属分类的信息为："+attrGroup.toString());
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
